﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.Windows.Controls.DataVisualization.Charting;
using System.Collections;
using System.ComponentModel;
using SilverlightApp.data;
using System.Windows.Data;
using System.Windows.Threading;
using System.Collections.ObjectModel;
using SilverlightApp.Web;
using SilverlightApp.Common;

namespace SilverlightApp
{
    public partial class GraphTest : Page
    {
        public GraphTest()
        {
            InitializeComponent();
            Start();
        }

        // 当用户导航到此页面时执行。
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        string zoneId = "hangzhou_001";
        string previousTime = "";
        MyObservableCollection<PairInfo> temperatureHistroy = new MyObservableCollection<PairInfo>();
        MyObservableCollection<PairInfo> humidityHistroy = new MyObservableCollection<PairInfo>();
        MyObservableCollection<PairInfo> illuminationHistroy = new MyObservableCollection<PairInfo>();
        MyObservableCollection<PairInfo> observableTemperature = new MyObservableCollection<PairInfo>();
        MyObservableCollection<PairInfo> observableHumidity = new MyObservableCollection<PairInfo>(); MyObservableCollection<PairInfo> observableIllumination = new MyObservableCollection<PairInfo>();
        List<MyObservableCollection<PairInfo>> dataHistroy = new List<MyObservableCollection<PairInfo>>();

        private void Start()
        {
            BuildGraphs();
            wc.GetMonitorDataWintZoneIdCompleted += new EventHandler<SilverlightAndAspdotNetMVC.ServiceReference1.GetMonitorDataWintZoneIdCompletedEventArgs>(wc_GetMonitorDataWintZoneIdCompleted);
            StartTimer();
        }

        private void ShowDebugInfo(string str, bool append)
        {
            if (append)
            {
                //   str = str + Environment.NewLine + debugInfo.Text;
            }
            // debugInfo.Text = str;
        }

        private void BuildGraphs()
        {
            DebugVersion.Text = "Debug version:004";

            TemperatureChartBuilder2 tcb = new TemperatureChartBuilder2();
            tcb.BuildChart(TemperatureChartsPanel, observableTemperature);

            HumidityChartBuilder2 hcb = new HumidityChartBuilder2();
            hcb.BuildChart(HumidityChartsPanel, observableHumidity);

            IlluminationChartBuilder2 icb = new IlluminationChartBuilder2();
            //icb.WarningMaxValue = 200;
            //icb.WarningMinValue = 50;
            icb.BuildChart(IlluminationChartsPanel, observableIllumination);

            dataHistroy.Add(temperatureHistroy);
            dataHistroy.Add(humidityHistroy);
            dataHistroy.Add(illuminationHistroy);
            LinearChartBuilder lcb = new LinearChartBuilder();
            lcb.BuildChart(DetailMonitorChartPanel, dataHistroy);
        }

        private void StartTimer()
        {
            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Interval = TimeSpan.FromSeconds(1);

            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick2);
            dispatcherTimer.Start();
        }

        SilverlightAndAspdotNetMVC.ServiceReference1.WebService1SoapClient wc = new SilverlightAndAspdotNetMVC.ServiceReference1.WebService1SoapClient();

        void dispatcherTimer_Tick2(object sender, EventArgs e)
        {
            if (zoneId != null)
            {
                wc.GetMonitorDataWintZoneIdAsync(zoneId);
            }
            return;
        }

        /// <summary>
        /// 使用服务取得数据库数据之后执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void wc_GetMonitorDataWintZoneIdCompleted(object sender, SilverlightAndAspdotNetMVC.ServiceReference1.GetMonitorDataWintZoneIdCompletedEventArgs e)
        {
            SilverlightAndAspdotNetMVC.ServiceReference1.MonitorDataEntity ret = null;
            ret = e.Result;
            if (!ret.Success)
            {
                return;
            }
            if (ret.ExportTime.Equals(previousTime)) //根据时间判断是否是最新数据
            {
                return;
            }
            previousTime = ret.ExportTime;
            AddToList(ret);
        }

        private void AddToList(SilverlightAndAspdotNetMVC.ServiceReference1.MonitorDataEntity lastData)
        {
            DateTime time = DateTime.ParseExact(lastData.ExportTime, "yyyyMMddHHmmss", null);
            temperatureHistroy.Add(new PairInfo() { Time = time, Value = lastData.Temperature });
            humidityHistroy.Add(new PairInfo() { Time = time, Value = lastData.Humidity });
            illuminationHistroy.Add(new PairInfo() { Time = time, Value = lastData.Illumination });
            BuildObservableCollection(observableTemperature, new PairInfo() { Name = "温度", Value = lastData.Temperature });
            BuildObservableCollection(observableHumidity, new PairInfo() { Name = "湿度", Value = lastData.Humidity });
            BuildObservableCollection(observableIllumination, new PairInfo() { Name = "光照", Value = lastData.Illumination });
        }

        private void BuildObservableCollection(MyObservableCollection<PairInfo> coll, PairInfo pairInfo)
        {
            if (coll.Count == 0)
            {
                coll.Add(pairInfo);
            }
            else
            {
                coll[0].Value = pairInfo.Value;
                if (coll.AfterPropertyChanged != null)
                {
                    coll.AfterPropertyChanged(coll);
                }
                while (1 < coll.Count)
                {
                    coll.RemoveAt(0);
                }
            }
        }
    }
}
